/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BrowseFormsDialog.java
 *
 * Created on Mar 3, 2012, 3:45:18 PM
 */
package mentalhealthliberia;

import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author greg
 */
public class BrowseFormsDialog extends javax.swing.JDialog {

    private MentalHealthLiberiaView parent;
    
    private PatientEncounterFormModel model;
    
    private ListSelectionModel listSelectionModel;
    
    public class PatientEncounterFormModel extends DefaultListModel {
        public PatientEncounterForm get(int index) {
            return (PatientEncounterForm)super.get(index);
        }
    }
    
    private void buildPatientEncounterFormModel(List<PatientEncounterForm> forms) {
        model = new PatientEncounterFormModel();
        for (PatientEncounterForm form : forms) {
            model.addElement(form);
        }
    }
    
    public ListModel getPatientEncounterFormModel() {
        return model;
    }
    
    private void createSelectionModel() {
        listSelectionModel = new DefaultListSelectionModel();
        listSelectionModel.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting() == false) {
                    PatientEncounterForm selectedForm = model.get(e.getFirstIndex());
                    parent.showForm(selectedForm);
                    setVisible(false);
                }
            }
        });
        listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    public ListSelectionModel getListSelectionModel() {
        return this.listSelectionModel;
    }
    
    /** Creates new form BrowseFormsDialog */
    public BrowseFormsDialog(MentalHealthLiberiaView parent, boolean modal, List<PatientEncounterForm> forms) {
        super(parent.getFrame(), modal);
        this.parent = parent;
        buildPatientEncounterFormModel(forms);
        createSelectionModel();
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mentalhealthliberia.MentalHealthLiberiaApp.class).getContext().getResourceMap(BrowseFormsDialog.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        getContentPane().add(jLabel1, gridBagConstraints);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(getPatientEncounterFormModel());
        jList1.setName("jList1"); // NOI18N
        jList1.setSelectionModel(getListSelectionModel());
        jScrollPane1.setViewportView(jList1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        getContentPane().add(jLabel2, gridBagConstraints);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        getContentPane().add(jButton1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
